from skimage import io,transform
import glob
import os
import tensorflow as tf
import numpy as np
import time

path = r'flower_photos/'#数据存放路径
model_path = './medl/'#模型保存路径

#设置图像处理之后的大小(由于是RGB格式数据，长宽高分别是100*100*3)
w = 100
h = 100
c = 3

def read_img(path):
    #推到式
    cate =  [path+x for x in os.listdir(path) if os.path.isdir(path+x)]
    # print(cate)
    imgs = []
    labels = []
    for idx,folder in enumerate(cate):
        for im in glob.glob(folder+'/*.jpg'):      #读取文件
            # print('reading the images:%s'%(im))
            img = io.imread(im)
            img = transform.resize(img,(w,h))
            imgs.append(img)
            labels.append(idx)
    return np.asarray(imgs,np.float32),np.asarray(labels,np.float32)
# read_img(path)
def divide_train_test(data,labels,ratio = 0.8): #将所有数据划分为训练集和测试集
    idx = np.int(data.shape[0]*ratio)
    x_train = data[:idx]
    y_train = label[:idx]
    x_val = data[idx:]
    y_val = label[idx:]
'''
打乱数据集，防止数据分布对结果的影响
'''
data = read_img(path)
label = read_img(path)
num_example=data.shape[0]
arr = np.arange(num_example)
np.random.shuffle(arr)
data = data[arr]
label = label[arr]

ratio = 0.8
s = np.int(num_example*ratio)
x_train = data[:s]
y_train = label[:s]
x_val = data[s:]
y_val = label[s:]

x=tf.placeholder(tf.float32,shape=[None,w,h,c],name='x')
y_=tf.placeholder(tf.int32,shape=[None,],name='y_')
'''
搭建神经网络
创建卷积神经网络模型，该模型是整个实验的核心，原始模型只有5层隐藏层，包括两个卷积层，两个池化层，一个全连接层
'''
def model(input_tensor,train,regularizer):
    #第一层卷积层，输入100*100*3，输出100*100*32
    with tf.variable_scope('layer1-conv1'):
        #权重
        conv1_weights = tf.get_variable('weight',[5,5,3,32],initializer=tf.truncated_normal_initializer(stddev=0.1))
        #偏执
        conv1_biases = tf.get_variable("bias",[32],initializer=tf.constant_initializer(0.0))
        #卷积
        conv1 = tf.nn.conv2d(input_tensor,conv1_weights,strides=[1,1,1,1],padding='SAME')
        #激励函数
        relu1 =tf.nn.relu(tf.nn.bias_add(conv1,conv1_biases))
    #第二层池化层，输入100*100*32  输出50*50*32   使用2*2的核做池化
    with tf.name_scope("layer2-pool1"):
        pool1 = tf.nn.max_pool(relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')

    with tf.variable_scope("layer3-conv2"):
        conv2_weights = tf.get_variable("weight",[5,5,32,64],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv2_biases = tf.get_variable("bias",[64],initializer=tf.constant_initializer(0.0))
        conv2 = tf.nn.conv2d(pool1,conv2_weights,strides=[1,1,1,1],padding='SAME')
        relu2 = tf.nn.relu(tf.nn.bias_add(conv2,conv2_biases))

    with tf.name_scope("layer4-pool2"):
        pool2 = tf.nn.max_pool(relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')


    with tf.variable_scope("layer5-conv3"):
        conv3_weights = tf.get_variable("weight",[5,5,64,128],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv3_biases = tf.get_variable("bias",[128],initializer=tf.constant_initializer(0.0))
        conv3 = tf.nn.conv2d(pool2,conv3_weights,strides=[1,1,1,1],padding='SAME')
        relu3 = tf.nn.relu(tf.nn.bias_add(conv3,conv3_biases))

    with tf.name_scope("layer6-pool3"):
        pool3 = tf.nn.max_pool(relu3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')

    with tf.variable_scope("layer7-conv4"):
        conv4_weights = tf.get_variable("weight",[5,5,128,128],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv4_biases = tf.get_variable("bias",[128],initializer=tf.constant_initializer(0.0))
        conv4 = tf.nn.conv2d(pool3,conv4_weights,strides=[1,1,1,1],padding='SAME')
        relu4 = tf.nn.relu(tf.nn.bias_add(conv4,conv4_biases))

    with tf.name_scope("layer8-pool4"):
        pool4 = tf.nn.max_pool(relu4,ksize=[1,2,2,1],strides=[1,2,2,1],padding="VALID")

        #layer8层的输出是矩阵：[6,6,128],layer9的输入时向量，所以需要把layer的输出转化为矩阵
        nodes = 6*6*128
        reshaped = tf.reshape(pool4,[-1,nodes])
        print("shape of reshaped:",reshaped.shape)

    with tf.variable_scope("layer9-fc1"):
        fc1_weights = tf.get_variable("weight",[nodes,1024],initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None:
            tf.add_to_collection("losses",regularizer(fc1_weights))
            #给全连接层的权重添加正则项：tf.add_to_collection函数可以把变量放入一个集合，把很多变量变成一个列表
            fc1_biases = tf.get_variable("bias",[1024],initializer=tf.constant_initializer(0.1))
            fc1 = tf.nn.relu(tf.matmul(reshaped,fc1_weights)+fc1_biases)
            if train:
                fc1 = tf.nn.dropout(fc1,0.5)

    with tf.variable_scope("layer10-fc2"):
        fc2_weights = tf.get_variable("weight",[1024,512],initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None:
            tf.add_to_collection("losses",regularizer(fc2_weights))
            #给全连接层的权重添加正则项：tf.add_to_collection函数可以把变量放入一个集合，把很多变量变成一个列表
            fc2_biases = tf.get_variable("bias",[512],initializer=tf.constant_initializer(0.1))
            fc2 = tf.nn.relu(tf.matmul(fc1,fc2_weights)+fc2_biases)
            if train:
                fc2 = tf.nn.dropout(fc2,0.5)

    with tf.variable_scope("layer11-fc3"):
        fc3_weights = tf.get_variable("weight",[512,5],initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None:
            tf.add_to_collection("losses",regularizer(fc3_weights))
            #给全连接层的权重添加正则项：tf.add_to_collection函数可以把变量放入一个集合，把很多变量变成一个列表
            fc3_biases = tf.get_variable("bias",[5],initializer=tf.constant_initializer(0.1))
            logit = tf.matmul(fc2,fc3_weights) + fc3_biases
    return logit

'''
定义正则项：用于防止过拟合，提升模型的泛化能力
'''
regularizer = tf.contrib.layers.l2_regularizer(0.001)

logits = model(x,False,regularizer)
print("shape of logits:",logits.shape)

b = tf.constant(value=1,dtype=tf.float32)
logits_eval = tf.multiply(logits,b,name='logits_eval')

'''
定义损失函数，定义优化器，定义训练运算，将loss最小化

'''
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,labels=y_)
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
train_op = optimizer.minimize(loss)
correct_prediction = tf.equal(tf.cast(tf.argmax(logits,1),tf.int32),y_)
acc = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

'''
定义一个函数，按批次取数据 
#从训练集获取数据，最后一个参数表示打乱
'''
def minibatch(inputs,lables,batch_size,shuffle=False):
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0,len(inputs) - batch_size + 1,batch_size):
        if shuffle:
            excerpt = indices[start_idx:start_idx+batch_size]
        else:
            excerpt = slice(start_idx,start_idx+batch_size)
        yield inputs[excerpt],lables[excerpt]
def minibatches(inputs=None,targets=None,batch_size=None,shuffle=False):
    assert  len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
    for start_idx in range(0,len(inputs) - batch_size + 1,batch_size):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batch_size]
        else:
            excerpt = slice(start_idx,start_idx+batch_size)
        yield inputs[excerpt],targets[excerpt]

n_epoch = 2
batch_size = 64

saver = tf.train.Saver()

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for epoch in range(n_epoch):
    print("epoch:",epoch+1)
    start_time = time.time()

    #traing
    train_loss,train_acc,n_batch = 0,0,0
    for x_train_a,y_train_a in minibatches(x_train,y_train,batch_size,shuffle=True):
        _,err,ac = sess.run([train_op,loss,acc],feed_dict={x:x_train_a,y:y_train_a})
        train_loss += err;train_acc += ac;n_batch += 1
    print(" train loss:%f"%(np.sum(train_loss)/n_batch))
    print(" train acc:%f"%(np.sum(train_acc)/n_batch))

    #validaton
    val_loss,val_acc,n_batch = 0,0,0
    for x_val_a,y_val_a in minibatches(x_val,y_val,batch_size,shuffle=False):
        err,ac = sess.run([loss,acc],feed_dict={x:x_val_a,y_:y_val_a})
        val_loss += err;val_acc += ac;n_batch += 1
    print(" validation loss %f:"%(np.sum(val_loss)/n_batch))
    print(" validation acc %f:"%(np.sum(val_acc)/n_batch))
    print(" epoch time %f:"%(time.time()-start_time))
    print('-------------------------------------------------------')

#保存和复原模型
saver.save(sess,model_path)

sess.close()

# 从原始数据的每一个类别中各自随机抽取一张图片进行模型验证
path1 = "flower_photos/daisy/5547758_eea9edfd54_n.jpg"
path2 = "flower_photos/dandelion/7355522_b66e5d3078_m.jpg"
path3 = "flower_photos/roses/394990940_7af082cf8d_n.jpg"
path4 = "flower_photos/sunflowers/6953297_8576bf4ea3.jpg"
path5 = "flower_photos/tulips/10791227_7168491604.jpg"

#定义花类字典，对没种花都复制一个数值类别
flower_dict = {0: 'dasiy', 1: 'dandelion', 2: 'roses', 3: 'sunflowers', 4: 'tulips'}

#定义转换后测试花类图像大小（长宽高分别是100，100，3
w = 100
h = 100
c = 3

#定义read_one_image函数，用于将验证图像转换成同意大小格式
def read_one_image(path):
    img = io.imread(path)
    img = transform.resize(img, (w, h))
    return np.asarray(img)


with tf.Session() as sess: #创建会话  用于执行已经定义的运算
    data = []                   #定义空白列表，用于保存处理后的验证数据
    data1 = read_one_image(path1)  #利用自定义函数read_one_image依次对5张验证图像进行格式标准化处理
    data2 = read_one_image(path2)
    data3 = read_one_image(path3)
    data4 = read_one_image(path4)
    data5 = read_one_image(path5)
    data.append(data1)
    data.append(data2)
    data.append(data3)
    data.append(data4)
    data.append(data5)
#利用import_meta_graph函数加载之前已经持久化的模型内容
    saver = tf.train.import_meta_graph('medl/model.ckpt.meta')
    #利用tf.train.latest_checkpoint提取最近一次保存的模型
    saver.restore(sess, tf.train.latest_checkpoint('medl'))

    graph = tf.get_default_graph()
    x = graph.get_tensor_by_name("x:0")
    feed_dict = {x: data}

    logits = graph.get_tensor_by_name("logits_eval:0")

    classification_result = sess.run(logits, feed_dict)

    # 打印出预测矩阵
    print(classification_result)
    # 打印出预测矩阵每一行最大值的索引
    print(tf.argmax(classification_result, 1).eval())
    # 根据索引通过字典对应花的分类
    output = []
    output = tf.argmax(classification_result, 1).eval()
    print(output)
    print(output.shape)
    for i in range(len(output)):
        print("flower", i + 1, "prediction:" + flower_dict[output[i]])

